<?php
class Core_Model_Url {
	
	protected $_request;
	
	protected $_queryParams = array();

	protected $_routeParams = array();

	protected $_routePath;

	public function __construct() {
		$this->_request = Mag::getRequest();
	}
	
	public function getRoutePath() {
		return $this->_routePath;
	}

	public function setRoutePath($val) {
		$parts = array_filter(explode('/', $val));
		$routePath = '';

		if (!empty($parts)) {
			$frontname = array_shift($parts);
			if ('*' === $frontname) {
				$frontname = $this->_request->getFrontName();
			}
			$routePath = $frontname . '/';
		}

		if (!empty($parts)) {
			$controller = array_shift($parts);
			if ('*' === $controller) {
				$controller = $this->_request->getControllerName();
			}
			$routePath .= $controller . '/';
		}

		if (!empty($parts)) {
			$action = array_shift($parts);
			if ('*' === $action) {
				$action = $this->_request->getActionName();
			}
			$routePath .= $action . '/';
		}

		if (!empty($parts)) {
			while (!empty($parts)) {
				$key = array_shift($parts);
				if (!empty($parts)) {
					$value = array_shift($parts);
					$this->setRouteParam($key, $value);
				}
			}
		}

		$this->_routePath = $routePath;
		return $this;	
	}
	
	public function setRouteParam($key, $val) {
		if ($val === null)
			unset($this->_routeParams[$key]);
		else
			$this->_routeParams[$key] = $val;
	}
	
	public function getRouteParam($key=null) {
		return $key === null ? $this->_routeParams :
			isset($this->_routeParams[$key]) ? $this->_routeParams[$key] : null;
	}
	
	public function setRouteParams(array $data, $clearOldData = true) {
        if ($clearOldData) $this->_routeParams = array();
		$this->_routeParams = array_merge($this->_routeParams, $data);
	}
	
	public function setQueryParam($key, $val) {
		if ($val === null)
			unset($this->_queryParams[$key]);
		else
			$this->_queryParams[$key] = urlencode($val);
	}
	
	public function setQueryParams(array $data, $clearOldData = true) {
        if ($clearOldData) $this->_queryParams = array();
		foreach ($data as $k => $v) {
			$this->setQueryParam($k, $v);
		}
	}
	
	public function getQueryParam($key) {
		return $key === null ? $this->_queryParams :
			isset($this->_queryParams[$key]) ? $this->_queryParams[$key] : null;
	}
	
	protected function _buildQuery() {
		if (empty($this->_queryParams)) return '';
		return '?' . http_build_query($this->_queryParams);
	}

	protected function _buildRouteParams() {
		$result = '';
		foreach($this->_routeParams as $k => $v) {
			$result .= "$k/$v/";
		}
		return $result;
	}

	public function getUrl($routePath = null, $routeParams = null) {
		$escape = true;
		if (isset($routeParams['_escape'])) {
			$escape = (bool)$routeParams['_escape'];
			unset($routeParams['_escape']);
		}

		if (isset($routeParams['_current'])) {
			$current = (bool)$routeParams['_current'];
			if ($current) {
				$this->setQueryParams($this->_request->getQuery());
			}
			unset($routeParams['_current']);
		}

		if (isset($routeParams['_query'])) {
			$query = $routeParams['_query'];
			$this->setQueryParams($query);
			unset($routeParams['_query']);
		}

        if (!is_null($routePath)) {
            $this->setRoutePath($routePath);
        }
		
        if (is_array($routeParams)) {
            $this->setRouteParams($routeParams, false);
        }
		
        $url = $this->getBaseUrl();

		// rewrite url
		
		if ($this->getRoutePath()) {
			if (substr($this->getBaseUrl(), -1) != '/')
				$url .= '/';

			if (!$this->isModRewriteEnabled())
				$url .= 'index.php/';
		}

		// build url
		$url .= $this->getRoutePath() 
			. $this->_buildRouteParams()
			. $this->_buildQuery();
		
		return $escape ? htmlspecialchars($url) : $url;
	}

	public function getBaseUrl() {
		return $this->_request->getBaseUrl();
	}
	
	public function isModRewriteEnabled() {
		// HTTP_MOD_REWRITE is set on htaccess file
		return getenv('HTTP_MOD_REWRITE')=='On' ? true : false;
		/*
		$result = false;
		if (function_exists('apache_get_modules')) {
			$modules = apache_get_modules();
			$result = in_array('mod_rewrite', $modules);
		} else {
			$result = getenv('HTTP_MOD_REWRITE')=='On' ? true : false ;
		}
		return $result;
		*/
	}
}